package GUILayer;
import ControlLayer.CtrCategory;
import ModelLayer.Category;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUICategory extends javax.swing.JFrame {
    private CtrCategory ctrCategory;

    public GUICategory() {
        initComponents();
        ctrCategory = CtrCategory.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblCategoryList.getModel()).setRowCount(0);
        ArrayList<Category> categoryList = ctrCategory.getCategoryList();
        for (Category category : categoryList) {
            String productIDs = new String();
            for (Integer productID : category.getProductIDs()) {
                productIDs += productID + ", ";
            }
            String discountIDs = new String();
            for (Integer discountID : category.getDiscountIDs()) {
                discountIDs += discountID + ", ";
            }
            ((DefaultTableModel)tblCategoryList.getModel()).addRow(
                new Object [] {
                    category.getIDCategory(),
                    category.getIDParent(),
                    category.getLevelDepth(),
                    category.getActive(),
                    category.getSort(),
                    category.getDateAdd(),
                    category.getDateUpd(),
                    category.getName(),
                    category.getDescription(),
                    category.getLinkRewrite(),
                    category.getMetaTitle(),
                    category.getMetaKeywords(),
                    category.getMetaDescription(),
                    productIDs,
                    discountIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlCategoryList = new javax.swing.JScrollPane();
        tblCategoryList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblCategoryList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_parent", "level_depth", "active", "sort", "date_add", "date_upd", "name", "description", "link_rewrite", "meta_title", "meta_keywords", "meta_description", "product IDs", "discount IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlCategoryList.setViewportView(tblCategoryList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlCategoryList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlCategoryList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUICategory().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlCategoryList;
    private javax.swing.JTable tblCategoryList;
    // End of variables declaration//GEN-END:variables
}
